package com.cooper.base.service;

import com.cooper.base.model.SysUser;
import com.cooper.common.constant.SpMyShiroConstant;
import com.cooper.common.core.db.QueryPage;
import com.cooper.common.core.service.BaseService;
import com.cooper.common.exception.GeneralException;
import com.cooper.common.util.DateUtils;
import com.cooper.common.util.PasswordUtil;
import com.cooper.common.util.PreDefineUser;
import com.cooper.common.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Copyright © 2017 com. All rights reserved.
 *
 * @Title: SysUserService.java
 * @Prject: springmvc-mybatis-shiro
 * @Package: com.cooper.base.service
 * @Description: TODO
 * @author: JackCooperZ
 * @date: 2017年1月17日 下午4:15:11
 * @version: V1.0
 */
@Service("com.cooper.base.service.SysUserService")
public class SysUserService extends BaseService<SysUser> {

    @Transactional(rollbackFor = Exception.class)
    public SysUser add(SysUser sysUser) {
        initAdd(sysUser);
        if (mapper.insert(sysUser) != 1) {
            throw new GeneralException("添加用户失败");
        }
        return sysUser;
    }

    @Transactional(rollbackFor = Exception.class)
    public SysUser update(SysUser sysUser, SysUser data) {
        initUpdate(sysUser, data);
        if (mapper.updateByPrimaryKeySelective(sysUser) != 1) {
            throw new GeneralException("更新用户失败");
        }
        mapper.updateByPrimaryKeySelective(sysUser);
        return sysUser;
    }

    /**
     * 用户列表
     */
    public PageInfo<SysUser> list(QueryPage page) {
        Example example = new Example(SysUser.class);
        example.createCriteria().andEqualTo("isDelete", false);
        PageHelper.startPage(page.getNextPage(), page.getiDisplayLength());
        List<SysUser> userList = mapper.selectByExample(example);
        return new PageInfo<>(userList);
    }

    private void initAdd(SysUser sysUser) {
        sysUser.setUserId(PasswordUtil.generateUserId());
        sysUser.setCreateBy(PreDefineUser.USER_ID);
        sysUser.setCreateDate(DateUtils.now());
        sysUser.setStatus(SpMyShiroConstant.UN_LOCKED);
        sysUser.setIsDelete(SpMyShiroConstant.UN_DELETED);
        sysUser.setSalt(PasswordUtil.generaterSalt());
        sysUser.setUpdateBy(PreDefineUser.USER_ID);
        sysUser.setUpdateDate(DateUtils.now());
        sysUser.setPassword(PasswordUtil.encryptBySalt(sysUser.getPassword(), sysUser.getSalt()));
    }

    private void initUpdate(SysUser sysUser, SysUser data) {
        if (StringUtil.isNotEmptyString(data.getRealName())) {
            sysUser.setRealName(data.getRealName());
        }
        if (StringUtil.isNotEmptyString(data.getEmail())) {
            sysUser.setEmail(data.getEmail());
        }
        if (StringUtil.isNotEmptyString(data.getRemarks())) {
            sysUser.setRemarks(data.getRemarks());
        }
    }
}
